<?php
// +----------------------------------------------------------------------
// | 随手拍
// +----------------------------------------------------------------------
// | Author: Helly  QQ:15655663
// +----------------------------------------------------------------------
// | Date: 2014/01/20 10:13
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;
use Think\Controller;

class CopyController extends Base {

    //随手拍首页
    public function index(){

        //中间广告位
        $this->assign('midad',parent::getAdvert(4,1,0,1));

        //作品列表
        $workid=I('get.wid');
        if(!empty($workid)){
            $where['copyid']=array('EQ',$workid);
            $copywhere=' and copyid='.$workid;
            $this->hbtitle=M('Works')->where(array('workid'=>$workid))->getField('title');
        }
        $worksModel=M('Works');
        $where['status']=array('EQ',true);
        $where['type']=array('EQ',2);
        $count = $worksModel->where($where)->count();
        $pageconfig=array();
        $pageconfig['prev']='上页';
        $pageconfig['next']='下页';

        $page = $this->page($count, 50, 0 ,$pageconfig);
        
        $Model=new Model();
        $list=$Model
            ->table(array(
                    C('DB_PREFIX').'works'=>'W',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('W.userid=U.userid and W.status=true and W.type=2'.$copywhere)
            ->field('W.title,W.content,W.workid,W.create_time,W.image,W.msgnum,W.goodnum,U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz')
            ->order('W.create_time DESC')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        $this->assign("list", $list);
        $this->assign("Page", $page->show());

        $this->display();
    }


    /**
     * 随拍作品详情
     */
    public function detail(){
        $workid=I('get.workid');
        if(empty($workid)){
            $this->error('您访问的页面不存在');
        }
        //增加点击数
        M('Works')->where(array('workid'=>$workid))->setInc('clicknum');
        $detail=D('Works')->relation(true)->find($workid);
        if(!$detail){
            $this->error('您访问的作品不存在');
        }
        $img_temp = explode("/", $detail['image']);
        $img_temp[count($img_temp)-1] = str_replace("yf_", "", $img_temp[count($img_temp)-1]);
        $detail['image'] = implode("/", $img_temp);
        
        $this->assign('data',$detail);
        
        $user = M('User')->where(array('userid' => $detail['userid']))->field("openuid, nickname, uavatar")->find();
        //print_r($user);
        $this->assign("user", $user);
        
        $copyinfo = M('Works')->where(array('workid' => $detail['copyid']))->field("title, image, userid")->find();
        $mname = M('WorksMaterials')->where(array('workid' => $detail['copyid']))->field("mname")->select();
        //print_r($mname);
        //print_r($copyinfo);
        $copyinfo['materials'] = array();
        if($mname)
        {
            $temp_mater = array();
            foreach($mname as $mk => $mv)
            {
                $temp_mater[] = $mv['mname'];
            }
            $copyinfo['materials'] = implode("、", $temp_mater);
            //$copyinfo['materials'] = "高筋粉（金像，焙食尚各一半）、细砂糖、可可粉、盐、干酵母、水（我们";
            $copyinfo['materials'] = hy_substr($copyinfo['materials'], 32, true);
        }
        
        $copyinfo['donum'] = M('Works')->where(array('copyid' => $detail['copyid']))->count();
        $seven = time() - 7 * 24 * 60 * 60;
        $copyinfo['nownum'] = M('Works')->where("copyid=%d and create_time > %s", array($detail['copyid'], $seven))->count();
        $this->assign("copyinfo", $copyinfo);
        
        $copyuser = M('User')->where(array('userid' => $copyinfo['userid']))->field("nickname, openuid")->find();
        $this->assign("copyuser", $copyuser);
        
        //点赞总数
        $this->digallnum=$detail['goodnum']+$detail['badnum'];

        //点赞判断
        $this->dignum=M('Dig')->where(array('userid'=>$this->userid,'status'=>true,'infoid'=>$workid))->count();

        //收藏判断
        $this->favoritenum=getCount('UserFavorite',array('userid'=>$this->userid,'status'=>true,'infoid'=>$workid));

        //右侧广告
        $this->rightad=parent::getAdvert(8,3,0,2);


        //点赞人员列表
        $diguser=new Model();
        $diguerlist=$diguser->table(array(
            C('DB_PREFIX').'dig'=>'D',
            C('DB_PREFIX').'user'=>'U',
        ))->where('D.userid=U.userid and D.status=true and D.digtype=1 and D.infoid='.$workid.'')
            ->field('U.uavatar,U.openuid')
            ->select();
        $this->assign('diglist',$diguerlist);

        //评论
//         $where['infoid']=$workid;
//         $where['type']=1;
//         $count = M('UserMessage')->where($where)->count();

//         $limitRows = 10; // 设置每页记录数
//         $page = $this->ajaxpage($count,$limitRows,'type=msg','ajax_div','ajax_page_div');

//         $msgModel=new Model();
//         $message=$msgModel
//             ->table(array(
//                     C('DB_PREFIX').'user_message'=>'M',
//                     C('DB_PREFIX').'user'=>'U',
//                 )
//             )
//             ->where('M.userid=U.userid and M.status=true and M.infoid='.$workid.' and M.type=1')
//             ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
//             ->order('M.create_time DESC')
//             ->limit($page->firstRow . ',' . $page->listRows)
//             ->select();

//         foreach($message as $k=>$v){
//             $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
//         }
        $where['infoid']=$workid;
        $where['type']=1;
        $where['parent_id'] = 0;
        $count = M('UserMessage')->where($where)->count();
        // $limitRows = 5; // 设置每页记录数
        $limitRows = C('YF_PAGE');
        $page = $this->ajaxpage($count,$limitRows,'#msg','ajax_div','ajax_page_div');
        
        $Model=new Model();
        $message=$Model
        ->table(array(
            C('DB_PREFIX').'user_message'=>'M',
            C('DB_PREFIX').'user'=>'U',
        )
        )
        ->where('M.userid=U.userid and M.status=true and M.infoid='.$workid.' and M.type=1 and M.parent_id=0')//yf 0912
        ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid,U.ifrz')
        ->order('M.create_time DESC')
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
        
        foreach($message as $k=>$v){
            $child_temp = array();
            $child_temp = $Model->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
        
            $message[$k]['childlist'] = array();
            if($child_temp)
            {
                foreach($child_temp as $yk => $yv)
                {
                    if($yv['userid'] == 0)
                    {
                        $child_temp[$yk]['nickname'] = "烘焙圈";
                        $child_temp[$yk]['openuid'] = 0;
                    }
                    else
                    {
                        $yf_user = array();
                        $yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
                        $child_temp[$yk]['nickname'] = $yf_user['nickname'];
                        $child_temp[$yk]['openuid'] = $yf_user['openuid'];
                        $child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
                        $child_temp[$yk]['ucity'] = $yf_user['ucity'];
                        $child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
                        $child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
                    }
                    if($yv['touserid'] != 0)
                    {
                        $child_temp[$yk]['touser'] = M('User')->field('openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
                    }
                    else
                    {
                        $child_temp[$yk]['touser']['nickname'] = "烘焙圈";
                        $child_temp[$yk]['touser']['openuid'] = 0;
                    }
                }
                $message[$k]['childlist'] = $child_temp;
            }
            $message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
            //$message[$k]['tempname'] = $v['nickname'];
            $message[$k]['touser'] = array();
            if(!empty($v['touserid']))
            {
                $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
            }
        }
        unset($where['parent_id']);
        $this->msgcount=M('UserMessage')->where($where)->count();
        $this->msgtype=1;
       // $this->msgcount=$count;
        $this->assign('msglist',$message);
        $this->assign('page',$page->show());

        if(IS_AJAX){
            exit($this->fetch('msglist'));//list为list.html模版
        }

        //获取会员烘焙作品数
        $this->hbworksnum=getCount('Works',array('status'=>true,'type'=>1,'userid'=>$detail['userid']));
        //获取会员随拍作品数
        $this->spworksnum=getCount('Works',array('status'=>true,'type'=>2,'userid'=>$detail['userid']));
        //是否关注
        $this->isfans=getCount('UserFans',array('myid'=>$this->userid,'userid'=>$detail['userid'],'status'=>true));

        //更多随拍作品
        $map['status']=array('EQ',true);
        $map['userid']=array('EQ',$detail['userid']);
        $map['workid']=array('NEQ',$workid);
        $this->moresp=M('Works')->field('image,workid,type,title')->where($map)->limit(9)->select();


        $this->display();
    }

    /**
     * 更多参考作品
     */
    public function clist(){
        $workid=I('get.wid');
        if(empty($workid)){
            $this->error('页面出现错误');
        }
        $this->title=M('Works')->where(array('workid'=>$workid))->getField('title');

        //中间广告位
        $this->assign('midad',parent::getAdvert(2,1,0,1));

        $worksModel=M('Works');
        $where['status']=array('EQ',true);
        $where['type']=array('EQ',2);
        $where['copyid']=array('EQ',$workid);

        $count = $worksModel->where($where)->count();
        $page = $this->Pages($count, 15);
        $pageurl='?wid='.$workid.'&page='.urlencode('[PAGE]');
        $page->url     = __APP__.'/copy/clist'.$pageurl;


        $Model=new Model();
        $list=$Model
            ->table(array(
                    C('DB_PREFIX').'works'=>'W',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('W.userid=U.userid and W.status=true and W.type=2 and W.copyid='.$workid)
            ->field('W.title,W.content,W.goodnum,W.msgnum,W.workid,W.create_time,W.image,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,U.ifrz')
            ->order('W.create_time DESC')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        foreach ($list as $key=>$value) {
            $list[$key]['dignum']=M('Dig')->where(array('status'=>true,'infoid'=>$value['workid'],'userid'=>$this->userid))->count();
        }

        $this->pagecount=$page->totalRows/$page->listRows;

        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->display();
    }

}